package com.agent.commons.other;


import com.agent.commons.enums.Role;
import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * 存储访问接口用户基本信息(线程安全)
 */
public class ThreadLocal {
    private static final TransmittableThreadLocal<String> userId = new TransmittableThreadLocal<>();
    private static final TransmittableThreadLocal<String> userName = new TransmittableThreadLocal<>();
    private static final TransmittableThreadLocal<String> role=new TransmittableThreadLocal<>();

    /**
     * 设置在线程中存储的用户id
     */
    public static void setUserId(String id){
        userId.set(id);
    }

    /**
     * 设置在线程中存储的用户名
     */
    public static void setUserName(String name){
        userName.set(name);
    }

    /**
     * 设置在线程中存储的用户角色
     * @param identity
     */
    public static void setRole(String identity){
        role.set(identity);
    }

    /**
     * 获取在线程中存储的用户id
     */
    public static String getUserId(){
        return userId.get();
    }

    /**
     * 获取在线程中存储的用户名
     */
    public static String getUserName(){
        return userName.get();
    }

    /**
     * 获取在线程中存储的用户角色
     * @return
     */
    public static String getRole(){
        return role.get();
    }

    /**
     * 移除在线程中存储的信息
     */
    public static void remove(){
        userId.remove();
        userName.remove();
    }
}
